package com.ctgu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.Evaluate;
import com.ctgu.model.entity.Experience;
import com.ctgu.model.entity.Files;
import com.ctgu.mapper.FilesMapper;
import com.ctgu.model.entity.LoginUser;
import com.ctgu.model.vo.ExperienceVo;
import com.ctgu.model.vo.FilesVo;
import com.ctgu.service.IEvaluateService;
import com.ctgu.service.IExperienceService;
import com.ctgu.service.IFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class
FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements IFilesService {

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private IExperienceService experienceService;

    @Override
    public ResponseResult getOneFiles(Integer id) {
        if(id==null){
            LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            id=loginUser.getUser().getId();
        }
        LambdaQueryWrapper<Experience> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Experience::getEId,id);
        FilesVo filesVo=new FilesVo();
        List<Experience> list = experienceService.list(wrapper);
        List< ExperienceVo> experienceVoList=list.stream().map(item->{
            ExperienceVo experienceVo=new ExperienceVo();
            BeanUtils.copyProperties(item, experienceVo);
            LambdaQueryWrapper<Evaluate> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(Evaluate::getCompanyName,item.getCompanyName());
            wrapper1.eq(Evaluate::getE_id,item.getEId());
            List<Evaluate> evaluateList=evaluateService.list(wrapper1);
            experienceVo.setEvaluateList(evaluateList);
            return experienceVo;
        }).collect(Collectors.toList());
        return new ResponseResult<>(200,"查询成功",experienceVoList);
    }


}
